import jsonp from 'jsonp'


const BASE_URL = 'https://apis.map.qq.com/ws/';
const URL_SEARCH = BASE_URL + 'place/v1/search';
const URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
const URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
const URL_CITY_LIST = BASE_URL + 'district/v1/list';
const URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
const URL_DISTANCE = BASE_URL + 'distance/v1/';


const key = "2CZBZ-RKRK3-HGJ3G-YH2G2-GIL3T-LHBFD"; //腾讯地图


const Utils = {

  createURL(url, param) {
    var urlLink = '';
    $.each(param, function (item, key) {
      var link = '&' + item + "=" + key;
      urlLink += link;
    })
    urlLink = url + "?" + urlLink.substr(1);
    return urlLink.replace(' ', '');
  }
}

class QQMapWX {

  getSuggestion(options) {
    var requestParam = {
      keyword: options.keyword,
      region: options.region || '全国',
      region_fix: options.region_fix || 0,
      policy: options.policy || 0,
      output: 'jsonp',
      key: key,
    };
    //jsonp方法会自动添加callback
    return new Promise((resolve, reject) => {
      jsonp(Utils.createURL(URL_SUGGESTION, requestParam), {}, (err, data) => {
        if (err) {
          console.log("tencent webservice api suggestion error ");
          console.log(err)
          reject(err)
        }
        if (data) {
          if (data.status === 0) {
            resolve(data.data)
          } else {
            reject(data.message);
          }
        }
        console.log(data)
      })
    });
  }

}
export default QQMapWX;